﻿namespace Alkitab
{
    using Microsoft.Phone.Controls;
    using System;
    using System.IO;
    using System.Windows;
    using System.Windows.Controls;

    public partial class Comment : PhoneApplicationPage
    {
        #region Fields

        public static int[] selected = { 0, 0, 0 };

        private int ijudul;

        #endregion Fields

        #region Constructors

        public Comment()
        {
            InitializeComponent();
            string[] xxx = changeTextToList("This. Lalal. St.Peter. Balala.");
            if (Globals.terjemahanlamakah) ijudul = Globals.nobuku; else ijudul = Globals.nobuku + 39;
            mh.ItemsSource = changeTextToList(changeText(getTextCom1(ijudul, Globals.nopasal, Globals.noayat)));
            pnt.ItemsSource = changeTextToList(changeText(getTextCom2(ijudul, Globals.nopasal, Globals.noayat)));
            jw.ItemsSource = changeTextToList(changeText(getTextWes(ijudul, Globals.nopasal, Globals.noayat)));
            jw.SelectedIndex = selected[0];
            pnt.SelectedIndex = selected[1];
            mh.SelectedIndex = selected[2];
            mh.FontSize = (int)Globals.Settingan["fontsize?"];
            pnt.FontSize = (int)Globals.Settingan["fontsize?"];
            jw.FontSize = (int)Globals.Settingan["fontsize?"];
        }

        #endregion Constructors

        #region Methods

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            // do some stuff ...
            selected[0] = 0;
            selected[1] = 0;
            selected[2] = 0;

            // cancel the navigation
            NavigationService.GoBack();
        }

        /**
        * Special Parser for commentary, this just removes the numbers before the verse
        *
        */

        private static String getTextCom1(int kk, int pp, int aa)
        {
            aa = 1; // in MH the commentary is always the first verse
            string k = Convert.ToString(kk);
            string p = Convert.ToString(pp);
            string a = Convert.ToString(aa);
            string tmp2 = "";
            bool startRead = false;
            char[] delimiterChars = { '\t' };
            String tempxx = "Commentaries/" + "cMH" + "/" + k + ".txt";
            var rs = Application.GetResourceStream(new Uri(tempxx, UriKind.Relative));
            StreamReader inFlow = new StreamReader(rs.Stream);
            while (startRead == false)
            {
                tmp2 = inFlow.ReadLine();

                string[] pecahan = tmp2.Split(delimiterChars);
                if (pecahan[2].Equals(p))
                {
                    if (pecahan[3].Equals(a))
                    {
                        return pecahan[4];
                    }
                }
            }
            return "This is a bug.";
        }

        private static String getTextCom2(int kk, int pp, int aa)
        {
            string k = Convert.ToString(kk);
            string p = Convert.ToString(pp);
            string a = Convert.ToString(aa);
            string tmp2 = "";
            bool startRead = false;
            char[] delimiterChars = { '\t' };
            String tempxx = "Commentaries/" + "cPNT" + "/" + k + ".txt";
            var rs = Application.GetResourceStream(new Uri(tempxx, UriKind.Relative));
            StreamReader inFlow = new StreamReader(rs.Stream);
            while (startRead == false)
            {
                tmp2 = inFlow.ReadLine();

                string[] pecahan = tmp2.Split(delimiterChars);
                if (pecahan[2].Equals(p))
                {
                    if (pecahan[3].Equals(a))
                    {
                        return pecahan[4];
                    }
                }
            }
            return "This is a bug.";
        }

        private string changeText(string x)
        {
            if (!((int)Globals.Settingan["languagek?"] == 2))
            {
                if (x.Equals("[no commentary for this verse]"))
                {
                    if ((int)Globals.Settingan["languagek?"] == 1) return
                        ("[tidak ada komentar untuk ayat ini]");
                    else
                        return
                            ("es gibt zu diesem Vers kein Kommentar");
                }
            }
            return (x);
        }

        private string[] changeTextToList(string x)
        {
            string y = x;
            int zzz = 0;
            for (int i = 0; i < x.Length - 1; i++)
            {
                if ((x[i] == '.') && (x[i + 1] == ' '))
                {
                    if ((zzz == 5))
                    {
                        y = y.Remove(i, 2);
                        y = y.Insert(i, ".°");
                        zzz = 0;
                    }
                    zzz++;
                }
            }

            string[] xx = y.Split('°');

            for (int i = 0; i < xx.Length; i++)
            {
                xx[i] = xx[i].Trim();
            }

            return xx;
        }

        /**
         * Special Parser for Wesley commentary
         *
         */

        private String getTextWes(int kk, int pp, int aa)
        {
            string k = Convert.ToString(kk);
            string p = Convert.ToString(pp);
            string a = Convert.ToString(aa);

            string tmp2 = "";
            bool startRead = false;
            int x = 1;
            char[] delimiterChars = { '\t' };
            String tempxx = "Commentaries/" + "cWSH" + "/" + k + ".txt";
            var rs = Application.GetResourceStream(new Uri(tempxx, UriKind.Relative));
            StreamReader inFlow = new StreamReader(rs.Stream);
            while (startRead == false)
            {
                tmp2 = inFlow.ReadLine();
                if (tmp2 == null) { return "[no commentary for this verse]"; }
                string[] pecahan = tmp2.Split(delimiterChars);
                if (pecahan[2].Equals(p))
                {
                    if (pecahan[3].Equals(a))
                    {
                        return pecahan[4];
                    }
                    if (!(pecahan[3].Equals(Convert.ToString(x))))
                    {
                        return "[no commentary for this verse]";
                    }
                    x++;
                }
            }

            return "This is a bug.";
        }

        private void jw_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selected[0] = jw.SelectedIndex;
        }

        private void mh_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selected[2] = mh.SelectedIndex;
        }

        private void pnt_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selected[1] = pnt.SelectedIndex;
        }

        private void x_Loaded(object sender, RoutedEventArgs e)
        {
            jw.ScrollIntoView(jw.SelectedItem);
            mh.ScrollIntoView(mh.SelectedItem);
            pnt.ScrollIntoView(pnt.SelectedItem);
        }

        #endregion Methods
    }
}